import { CHANGE_TEXT, ADD_TO_DO, CHANGE_CHECK, DEL_TASK } from './constans'
let init = {
  val: "", // todo需要输入的值；,
  list: []
};

export default function todoReducer(state = init, action) {
  if (action.type === CHANGE_TEXT) {
    return { ...state, val: action.payload };
  }
  if (action.type === ADD_TO_DO) {
    let arr = state.list;
    arr.push(action.payload)
    return { ...state, list: arr };
  }

  if (action.type === CHANGE_CHECK) {
    // 1.根据id 获取 list的index ，通过index修改 该条数据的 done属性
    let { id, checked } = action.payload;
    let index = state.list.findIndex(item => item.id === id);
    // 2.更改done的值 ；
    let arr = state.list;
    arr[index].done = checked;
    return { ...state, list: arr };
  }
  if (action.type === DEL_TASK) {
    let id = action.payload;
    console.log("??", id);
    let arr = state.list.filter(item => item.id !== id);
    return { ...state, list: arr };

  }


  return state;
}